package com.springcloudalibaba.serviceUser.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springcloudalibaba.bclass.base.BaseException;
import com.springcloudalibaba.bclass.base.BaseLoginUserInfo;
import com.springcloudalibaba.bclass.enums.SysStateEnum;
import com.springcloudalibaba.serviceUser.entity.admin.AdminEntity;
import com.springcloudalibaba.serviceUser.service.admin.AdminService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;



import java.io.IOException;
import java.text.MessageFormat;

/**
 * <p>
 * 系统管理员 前端控制器
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2021-06-06
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    /**
     * @描述 登录
     * @作者 lixing
     * @日期 2021/6/17 8:50
     * @Param [entity]
     * @return com.eureka.bclass.base.BaseResult<java.lang.String>
     */
    @PostMapping("login")
    public BaseLoginUserInfo login(@RequestBody BaseLoginUserInfo baseLoginUserInfo) {
        // 根据账号查询用户信息
        AdminEntity adminEntity = adminService.getBaseMapper().selectOne(new QueryWrapper<AdminEntity>()
                .select("pass", "active", "phone", "account")
                .eq("account", baseLoginUserInfo.getAccount()).or().eq("phone", baseLoginUserInfo.getAccount())
        );
        if (null == adminEntity) {
            throw new BaseException("账号不存在");
        }

        if (SysStateEnum.STATE_NO.getValue() == adminEntity.getActive()) {
            throw new BaseException(SysStateEnum.RESPONSE_STATUS_FALSE_USER_DISABLE.getDescribe());
        }
        //
        BaseLoginUserInfo baseLoginUserInfoResult = new BaseLoginUserInfo();
        baseLoginUserInfoResult.setAccount(adminEntity.getAccount());
        baseLoginUserInfoResult.setPhone(adminEntity.getPhone());
        baseLoginUserInfoResult.setPass(adminEntity.getPass());
        return baseLoginUserInfoResult;
    }

    /**
     * @描述 获取用户密码
     * @作者 lixing
     * @日期 2021/6/19 22:11
     * @Param []
     * @return com.eureka.bclass.base.BaseResult<java.lang.String>
     */
    @GetMapping("getUserPwd")
    public String getUserPwd(@RequestParam String account) {
        AdminEntity adminEntity = adminService.getOne(new QueryWrapper<AdminEntity>().select("pass").eq("account", account));
        return null == adminEntity ? null : adminEntity.getPass();
    }

}

